package com.caul.demo.hadoop.mapreduce.rank;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Created by sdliang on 2018/4/11.
 */
public class RankKey implements WritableComparable<RankKey> {

  private String uid;
  private String movie;
  private int grade;

  @Override
  public int compareTo(RankKey o) {
    int flag = this.uid.compareTo(o.getUid());
    if (flag == 0) {
      flag = o.getGrade() - this.grade;
    }
    if (flag == 0) {
      flag = this.movie.compareTo(o.getMovie());
    }
    return flag;
  }

  @Override
  public void write(DataOutput out) throws IOException {
    out.writeUTF(getUid());
    out.writeUTF(getMovie());
    out.writeInt(getGrade());
  }

  @Override
  public void readFields(DataInput in) throws IOException {
    this.uid = in.readUTF();
    this.movie = in.readUTF();
    this.grade = in.readInt();
  }

  @Override
  public String toString() {
    return getUid() + "\t" + getMovie() + "\t" + getGrade();
  }

  public String getUid() {
    return uid;
  }

  public void setUid(String uid) {
    this.uid = uid;
  }

  public String getMovie() {
    return movie;
  }

  public void setMovie(String movie) {
    this.movie = movie;
  }

  public int getGrade() {
    return grade;
  }

  public void setGrade(int grade) {
    this.grade = grade;
  }
}
